package com.hjwoo.app.net;

import android.content.Context;

import com.google.gson.Gson;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangchuan on 2016/5/10.
 * 请求方法总汇
 */
public class RequestMethod
{
    private static RequestMethod requestMethod = null;

    private RequestMethod()
    {
    }

    public static RequestMethod getMethod()
    {
        if (requestMethod == null)
        {
            synchronized (RequestMethod.class)
            {
                if (requestMethod == null)
                {
                    requestMethod = new RequestMethod();
                }
            }
        }
        return requestMethod;
    }

    /**
     * 注册的方法
     *
     * @param cellphone
     * @param password
     * @param city
     */
    public static void doRegisterMsg(String fullName, String city, String cellphone, String password, String userCode, RequestListener requestListener, Context context)
    {

        RequestParams params = new RequestParams(RequestUrl.APPREGISTER);
        params.addBodyParameter("fullName", fullName);
        params.addBodyParameter("city", city);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("password", password);
        params.addBodyParameter("userCode", userCode);
        HttpTool.RequestPost(params, RequestTag.APPREGISTER, requestListener, context);
    }

    /**
     * 获取手机是否注册
     *
     * @param cellphone
     * @param requestListener
     */
    public static void doCellphoneIsRegistered(String cellphone, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPCELLPHONEISREGISTERED);
        params.addBodyParameter("cellphone", cellphone);
        HttpTool.RequestPost(params, RequestTag.APPCELLPHONEISREGISTERED, requestListener, context);
    }

    /**
     * 获取验证码
     *
     * @param cellphone
     * @param requestListener
     */
    public static void doSendVerifiedCode(String cellphone, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPSENDVERIFIEDCODE);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("operation", "Register");
        HttpTool.RequestPost(params, RequestTag.APPSENDVERIFIEDCODE, requestListener, context);

    }

    /**
     * 判断验证吗是否正确
     *
     * @param cellphone
     * @param verifiedCode
     * @param requestListener
     */
    public static void doVerifyCode(String cellphone, String verifiedCode, RequestListener requestListener, Context context)
    {

        RequestParams params = new RequestParams(RequestUrl.APPVERIFYCODE);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("verifiedCode", verifiedCode);
        HttpTool.RequestPost(params, RequestTag.APPVERIFYCODE, requestListener, context);
    }

    /**
     * 登录的方法
     *
     * @param cellphone
     * @param password
     * @param requestListener
     */
    public static void doLoginMsg(String cellphone, String password, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPLOGIN);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("password", password);
        HttpTool.RequestPost(params, RequestTag.APPLOGIN, requestListener, context);
    }

    /**
     * 获取小区
     *
     * @param city
     * @param district
     * @param sessionId
     * @param requestListener
     */
    public static void doQueryDistrict(String city, String district, String sessionId, RequestListener requestListener, Context context)
    {

        RequestParams params = new RequestParams(RequestUrl.APPQUERYDISTRICT);
        params.addBodyParameter("city", city);
        params.addBodyParameter("district", district);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPQUERYDISTRICT, requestListener, context);
    }

    /**
     * 获取房产估值
     *
     * @param city
     * @param district
     * @param building
     * @param floor
     * @param totalFloor
     * @param roomNumber
     * @param numberOfLivingRoom
     * @param orientation
     * @param area
     * @param houseType
     * @param specialFactors
     * @param sessionId
     * @param requestListener
     */

    public static void doQueryPrice(String city, String district, String building, String floor, String totalFloor,
                                    String roomNumber, String numberOfLivingRoom, String orientation, String area,
                                    String houseType, String specialFactors, String sessionId, RequestListener requestListener, Context context)
    {

        RequestParams params = new RequestParams(RequestUrl.APPQUERYPRICE);
        params.addBodyParameter("city", city);
        params.addBodyParameter("district", district);
        params.addBodyParameter("building", building);
        params.addBodyParameter("floor", floor);
        params.addBodyParameter("totalFloor", totalFloor);
        params.addBodyParameter("roomNumber", roomNumber);
        params.addBodyParameter("numberOfLivingRoom", numberOfLivingRoom);
        params.addBodyParameter("orientation", orientation);
        params.addBodyParameter("area", area);
        params.addBodyParameter("houseType", houseType);
        params.addBodyParameter("specialFactors", specialFactors);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPQUERYPRICE, requestListener, context);
    }

    /**
     * 保存寻值状态
     *
     * @param unitPrice
     * @param requestListener
     */
    public void appcreate(String unitPrice, String area, String specialFactors, String orientation, String roomNumber, String city,
                          String houseType, String loanAmount, String building, String numberOfLivingRoom, String district,
                          String totalFloor, String floor, RequestListener requestListener, Context context)
    {

        Map<String, String> map = new HashMap<>();
        map.put("unitPrice", unitPrice);
        map.put("area", area);
        map.put("specialFactors", specialFactors);
        map.put("orientation", orientation);
        map.put("roomNumber", roomNumber);
        map.put("city", city);
        map.put("houseType", houseType);
        map.put("loanAmount", loanAmount);
        map.put("building", building);
        map.put("numberOfLivingRoom", numberOfLivingRoom);
        map.put("district", district);
        map.put("totalFloor", totalFloor);
        map.put("floor", floor);

        Map map1 = new HashMap();
        map1.put("opportunity", map);
        map1.put("sessionId", LoanAccessTokenIdKeeper.readAccessToken(context).getAppSessionId());

        RequestParams params = new RequestParams(RequestUrl.APPCREATE);
        params.addBodyParameter("", new Gson().toJson(map1));
        HttpTool.RequestPost(params, RequestTag.APPCREATE, requestListener, context);
    }


    /**
     * 报单
     *
     * @param opportunity
     * @param requestListener
     */
    public void appSubmit(String opportunity, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPSUBMIT);
        params.addBodyParameter("", opportunity);
        HttpTool.RequestPost(params, RequestTag.APPSUBMIT, requestListener, context);
    }

    /**
     * 获取订单列表
     *
     * @param args
     * @param context
     * @param requestListener
     */
    public void queryByStageForFragment(String[] args, String[] args2, Context context, RequestListener requestListener)
    {

        HashMap      hm = new HashMap();
        List<String> ls = new ArrayList<>();
        for (int i = 0; i < args.length; i++)
        {
            ls.add(args[i]);
        }
        List<String> ls2 = new ArrayList<>();
        for (int i = 0; i < args2.length; i++)
        {
            ls2.add(args2[i]);
        }
        hm.put("stages", ls);
        hm.put("status", ls2);
        hm.put("sessionId", LoanAccessTokenIdKeeper.readAccessToken(context).getAppSessionId());
        Gson          gson   = new Gson();
        RequestParams params = new RequestParams(RequestUrl.APPQUERYBYSTAGEANDSTATUS);
        params.addBodyParameter("", gson.toJson(hm));
        HttpTool.RequestPost(params, RequestTag.QUERYBYSTAGEFORFRAGMENT, requestListener, context);
    }

    /**
     * 获取订单列表
     *
     * @param args
     * @param context
     * @param requestListener
     */
    public void queryByStage(String[] args, String[] args2, Context context, RequestListener requestListener)
    {
        HashMap      hm = new HashMap();
        List<String> ls = new ArrayList<>();
        for (int i = 0; i < args.length; i++)
        {
            ls.add(args[i]);
        }
        List<String> ls2 = new ArrayList<>();
        for (int i = 0; i < args2.length; i++)
        {
            ls2.add(args2[i]);
        }
        hm.put("stages", ls);
        hm.put("status", ls2);
        hm.put("sessionId", LoanAccessTokenIdKeeper.readAccessToken(context).getAppSessionId());
        Gson          gson   = new Gson();
        RequestParams params = new RequestParams(RequestUrl.APPQUERYBYSTAGEANDSTATUS);
        params.addBodyParameter("", gson.toJson(hm));
        HttpTool.RequestPost(params, RequestTag.QUERYBYSTAGE, requestListener, context);
    }


    /**
     * 找回密码
     *
     * @param cellphone
     * @param verifiedCode
     * @param password
     * @param requestListener
     */
    public void appUpdatePassword(String cellphone, String verifiedCode, String password, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPUPDATEPASSWORD);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("verifiedCode", verifiedCode);
        params.addBodyParameter("password", password);
        HttpTool.RequestPost(params, RequestTag.APPUPDATEPASSWORD, requestListener, context);
    }

    /**
     * 获取验证码
     *
     * @param cellphone
     * @param requestListener
     */
    public void appSendVerifiedCode(String cellphone, String operation, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPSENDVERIFIEDCODE);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("operation", operation);
        HttpTool.RequestPost(params, RequestTag.APPSENDVERIFIEDCODE, requestListener, context);
    }

//    /**
//     * 校验验证码
//     *
//     * @param cellphone
//     * @param verifiedCode
//     * @param requestListener
//     */
//    public void appVerifyCode(String cellphone, String verifiedCode, RequestListener requestListener,Context context) {
//        RequestParams params = new RequestParams(RequestUrl.APPVERIFYCODE);
//        params.addBodyParameter("cellphone", cellphone);
//        params.addBodyParameter("verifiedCode", verifiedCode);
//        HttpTool.RequestPost(params, RequestTag.APPVERIFYCODE, requestListener,context);
//    }

    /**
     * 获取个人信息
     *
     * @param sessionId
     * @param requestListener
     * @param context
     */
    public void appQueryContactBySessionId(String sessionId, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPQUERYCONTACTBYSESSIONID);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPQUERYCONTACTBYSESSIONID, requestListener, context);
    }

    /**
     * 修改个人信息
     *
     * @param sessionId
     * @param city
     * @param idNumber
     * @param bankName
     * @param bankAccount
     * @param context
     */
    public void appUpdateContact(String sessionId, String city, String idNumber, String bankName, String bankAccount, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPUPDATECONTACT);
        params.addBodyParameter("sessionId", sessionId);
        params.addBodyParameter("city", city);
        params.addBodyParameter("idNumber", idNumber);
        params.addBodyParameter("bankName", bankName);
        params.addBodyParameter("bankAccount", bankAccount);
        HttpTool.RequestPost(params, RequestTag.APPUPDATECONTACT, requestListener, context);
    }

    /**
     * 验证码登录
     * @param cellphone
     * @param verifiedCode
     * @param requestListener
     * @param context
     */
    public void appLoginWithCode(String cellphone, String verifiedCode, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPLOGINWITHCODE);
        params.addBodyParameter("cellphone", cellphone);
        params.addBodyParameter("verifiedCode", verifiedCode);
        HttpTool.RequestPost(params, RequestTag.APPLOGINWITHCODE, requestListener, context);
    }

    /**
     * 上传头像
     * @param sessionId
     * @param avatar
     * @param requestListener
     * @param context
     */
    public void appUploadAvatar(String sessionId, String avatar, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPUPLOADAVATAR);
        params.addBodyParameter("sessionId", sessionId);
        params.addBodyParameter("avatar", avatar);
        HttpTool.RequestPost(params, RequestTag.APPUPLOADAVATAR, requestListener, context);
    }

    /**
     * 获取城市列表
     * @param sessionId
     * @param requestListener
     * @param context
     */
    public void appQueryCity(String sessionId, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPQUERYCITY);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPQUERYCITY, requestListener, context);
    }

    /**
     * 已经预约列表
     * @param sessionId
     * @param requestListener
     * @param context
     */
    public void appQueryAllAppointmentByContact(String sessionId, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPQUERYALLAPPOINTMENTBYCONTACT);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPQUERYALLAPPOINTMENTBYCONTACT, requestListener, context);
    }

    /**
     * 上传身份证和房产
     * @param attachmentType
     * @param file
     * @param opportunitySerialNumber
     * @param requestListener
     * @param context
     */
    public void appUploadImgs( String attachmentType,String file,String opportunitySerialNumber, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.UPLOAD);
//        RequestParams params = new RequestParams("http://192.168.168.66:8080/attachments/upload");
        params.addBodyParameter("attachmentType", attachmentType);
        params.addBodyParameter("file", file);
        params.addBodyParameter("fileType", "jpg");
        params.addBodyParameter("opportunitySerialNumber", opportunitySerialNumber);//sdsdfsdfds
        HttpTool.RequestPost(params, RequestTag.UPLOAD, requestListener, context);
    }
    /**
     * 预约下户
     * @param sessionId
     * @param opportunitySerialNumber //订单编号
     * @param subtype
     * @param startTime
     * @param endTime
     * @param description
     * @param requestListener
     * @param context
     */
    public void appMakeAppointmentByContact(String sessionId,String opportunitySerialNumber,
                                            String subtype,String startTime,String endTime,
                                            String description, RequestListener requestListener,
                                            Context context)
    {
        //http://192.168.168.72:8080/activity/appMakeAppointmentByContact
        RequestParams params = new RequestParams(RequestUrl.APPMAKEAPPOINTMENTBYCONTACT);
        params.addBodyParameter("sessionId", sessionId);
        params.addBodyParameter("opportunitySerialNumber", opportunitySerialNumber);
        params.addBodyParameter("subtype",subtype);
        params.addBodyParameter("startTime", startTime);
        params.addBodyParameter("endTime", endTime);
        params.addBodyParameter("description", description);
        HttpTool.RequestPost(params, RequestTag.APPMAKEAPPOINTMENTBYCONTACT, requestListener, context);
    }

    /**
     * 判断身份证是否在保护期
     * @param sessionId
     * @param requestedAmount
     * @param fullName
     * @param loanDuration
     * @param requestListener
     * @param context
     */
//    let params: [String: AnyObject] = ["requestedAmount": opportunity["requestedAmount"]!,"fullName": opportunity["fullName"]!,"loanDuration":opportunity["loanDuration"]!,"sessionId": Service.getSession()!]
    public void appProjectTime(String sessionId, String requestedAmount,String fullName,String loanDuration, RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPPROJECTTIME);
        params.addBodyParameter("sessionId", sessionId);
        params.addBodyParameter("requestedAmount", requestedAmount);
        params.addBodyParameter("fullName", fullName);
        params.addBodyParameter("loanDuration", loanDuration);
        HttpTool.RequestPost(params, RequestTag.APPPROJECTTIME, requestListener, context);
    }

    /**
     * 判断是否有订单
     * @param requestListener
     * @param context
     */
    public void appCreateOpportunity( RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPCREATEOPPORTUNITY);
        LoanLog.e("test", "ddddd "+LoanAccessTokenIdKeeper.readAccessToken(context).getAppSessionId() );
        params.addBodyParameter("sessionId", LoanAccessTokenIdKeeper.readAccessToken(context).getAppSessionId());
        HttpTool.RequestPost(params, RequestTag.APPCREATEOPPORTUNITY, requestListener, context);
    }
    /**
     * 获取服务器时间
     * @param sessionId
     */
   /* public void appGetSystemTime( String sessionId,RequestListener requestListener, Context context)
    {
        RequestParams params = new RequestParams(RequestUrl.APPGETSYSTEMTIME);
        params.addBodyParameter("sessionId", sessionId);
        HttpTool.RequestPost(params, RequestTag.APPGETSYSTEMTIME, requestListener, context);
    }*/
}
